/** Copyright 2020 Zhejiang Lab and Zhejiang University. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
*     http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* =============================================================
*/


import { ROOT_URL } from '../../config'
import axios from 'axios'

// initial state
const state = {
    errors: "",
    metric_names: [],
    graphs: {},
}

// getters
const getters = {
}

// actions
const actions = {
    getMetricNames({ dispatch, commit, state }) {
        axios.get(`${ROOT_URL}/api/visualization/metric_names`, {
        })
            .then((response) => {
                console.log('get metric_names success')
                const metric_names = response.data
                commit('setMetricNames', metric_names)
                // dispatch("getGraphs", metric_names.slice(0,2))
            })
            .catch((errors) => {
                console.log("error", errors)
                commit('setMetricNames', [])
                commit('setErrors', errors)
            })
    },
    getGraphs({ commit, state }, metric_names) {
        console.log('get graph', metric_names)
        axios.get(`${ROOT_URL}/api/visualization/graph`, {
            params: {
                metric_names: metric_names
            }
        })
            .then((response) => {
                console.log('get graph success')
                commit('setGraphs', response.data)
            })
            .catch((errors) => {
                console.log("error", errors)
                commit('setGraphs', {})
                commit('setErrors', errors)
            })
    },
}

const mutations = {
    setErrors(state, errors) {
        state.errors = errors
    },
    setGraphs(state, graphs) {
        //for (var i = 0; i < graphs.Metric1.nodes.length; i++) { 
        //    graphs.Metric1.nodes[i].url = 'https://www.baidu.com'
        // }
        state.graphs = graphs
    },
    setMetricNames(state, metric_names) {
        state.metric_names = metric_names
    }
}

export default {
    namespaced: true,
    state,
    getters,
    actions,
    mutations
}